package com.getpebble.android.bluetooth.radio;

import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.os.Build.VERSION;
import com.getpebble.android.bluetooth.device.AbsDeviceService;
import com.getpebble.android.common.core.trace.Trace;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID;

public class PebbleBluetoothDevice {
    private static final UUID BLUETOOTH_SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private static final String TAG = PebbleBluetoothDevice.class.getSimpleName();
    private BluetoothDevice mBluetoothDevice;

    public static class BondInitFailedException extends Exception {
    }

    public PebbleBluetoothDevice(BluetoothDevice device) {
        this.mBluetoothDevice = device;
    }

    public String getAddress() {
        return this.mBluetoothDevice.getAddress();
    }

    public String getName() {
        return this.mBluetoothDevice.getName();
    }

    public BluetoothClass getBluetoothClass() {
        return this.mBluetoothDevice.getBluetoothClass();
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PebbleBluetoothDevice that = (PebbleBluetoothDevice) o;
        if (getAddress() != null) {
            if (getAddress().equals(that.getAddress())) {
                return true;
            }
        } else if (that.getAddress() == null) {
            return true;
        }
        return false;
    }

    public int hashCode() {
        return getAddress() != null ? getAddress().hashCode() : 0;
    }

    public boolean createBond() throws BondInitFailedException {
        if (isBonded()) {
            Trace.debug(TAG, "createBond(): already bonded");
            return false;
        } else if (VERSION.SDK_INT >= 19) {
            Trace.debug(TAG, "createBond()");
            ret = this.mBluetoothDevice.createBond();
            if (ret) {
                return ret;
            }
            throw new BondInitFailedException();
        } else {
            Trace.debug(TAG, "createBondLegacy()");
            try {
                ret = ((Boolean) this.mBluetoothDevice.getClass().getMethod("createBond", (Class[]) null).invoke(this.mBluetoothDevice, (Object[]) null)).booleanValue();
                Trace.debug(TAG, "legacy createBond reflection res = " + ret);
                if (ret) {
                    return ret;
                }
                throw new BondInitFailedException();
            } catch (NoSuchMethodException e) {
                Trace.error(TAG, "Error creating legacy bond", e);
                return false;
            } catch (InvocationTargetException e2) {
                Trace.error(TAG, "Error creating legacy bond", e2);
                return false;
            } catch (IllegalAccessException e3) {
                Trace.error(TAG, "Error creating legacy bond", e3);
                return false;
            }
        }
    }

    public PebbleBluetoothSocket createSocket() throws IOException {
        Trace.debug(TAG, "createSocket()");
        return new PebbleBluetoothSocket(this.mBluetoothDevice.createRfcommSocketToServiceRecord(BLUETOOTH_SERIAL_UUID), this.mBluetoothDevice);
    }

    public boolean isBonded() {
        if (this.mBluetoothDevice.getBondState() == 12) {
            return true;
        }
        if (!AbsDeviceService.getPebbleBluetoothAdapter().getBondedDevices().contains(this)) {
            return false;
        }
        Trace.verbose(TAG, "getBondState returned not bonded, but in bonded devices list...");
        return true;
    }

    public void acceptPairingRequest() {
        try {
            Trace.debug(TAG, "setPairingConfirmation() = " + ((Boolean) this.mBluetoothDevice.getClass().getMethod("setPairingConfirmation", new Class[]{Boolean.TYPE}).invoke(this.mBluetoothDevice, new Object[]{Boolean.valueOf(true)})).booleanValue());
        } catch (Exception e) {
            Trace.warning(TAG, "acceptPairingRequest() error for " + this.mBluetoothDevice.getAddress(), e);
        }
    }

    public boolean removeBond() {
        boolean z = false;
        try {
            z = ((Boolean) this.mBluetoothDevice.getClass().getMethod("removeBond", (Class[]) null).invoke(this.mBluetoothDevice, new Object[0])).booleanValue();
        } catch (Exception e) {
            Trace.warning(TAG, "Error calling removeBond() for " + this.mBluetoothDevice.getAddress(), e);
        }
        return z;
    }
}
